﻿/*
 *  * User: RDressing@gmail.com
 * Date: 2/15/2011
 * Time: 7:18 AM
 * 
 ServiceCommander - a commandable wrapper for a service controller
 
 * 
 * 
 * 
 */
using System;
using System.ServiceProcess;
using System.Collections.Generic;
using System.Windows.Forms;



namespace Utility.ServiceQuickChange
{
    /// <summary>
    /// ServiceCommander
    /// General commandable wrapper for Service Controller.
    /// - Renders a menu with name/status/togglestate
    /// - Commands Service Controller to toggle state
    /// </summary>
    public class ServiceCommander
    {
        //Service Controller
        System.ServiceProcess.ServiceController _sc= null;
       
        //Command state table
        private Dictionary<String,String> _commandstates =new Dictionary<string, string>() ;
        
        
        //valid service
        private bool _valid = false;
        
        public ServiceCommander(string ServiceName,  MenuItem ServiceMenu)
        {
            _sc = new ServiceController(ServiceName);   //will not throw for an invalid service
            
            //force validation 
            try
            {
                string test =_sc.DisplayName;
                _valid = true;
            }
            catch(Exception ex)
            {
                Exception NewEx = new Exception(ServiceName + " is not a valid service!", ex);
                throw NewEx;
                
            }
            
            this.DisplayName = DisplayName;

            
             _commandstates.Add("Running", "Stop");
             _commandstates.Add("Stopped","Start");
            
        }
        
        # region properties
        
        // Display Name
        public string DisplayName { get; set; }
        

        
        # endregion properties
        
        
        # region methods
        
        // returns the status of the underlying service
        public string GetStatus()
        {
            string ret = "Unknown";
            if (_valid)
            {
                _sc.Refresh();
                ret = _sc.Status.ToString();
                
            }
            return ret;
        }
        
        // provides the menu name based on service name and status
        public string GetServiceMenuName()
        {
            if (!_valid)
            {
                return "Unknown";
            }else
            {
                _sc.Refresh();
                return ( _sc.ServiceName + " - " + _sc.Status.ToString());
            }            
        }
        
        // provides the toggle command if available
        public string GetToggleCommandText()
        {
            string status = GetStatus();
                      
            string ret = "Not Commandable";
            if (_commandstates.ContainsKey(status))
            {
                ret = _commandstates[status];
                
            }
            return ret;
        }
         
        // attempts to control the service if the command is either
        // start or stop.   If the service is not valid, throws an exception.
        // if SC can not apply a new service state, the SC will throw        
         public void ApplyServiceCommandState( string Command)
        {
            if (!_valid) 
            {
                throw new Exception("Service is not Valid");
            }
            
           
            if (Command=="Start")
            {
                _sc.Start();
            }
            if (Command =="Stop")
            {
                _sc.Stop();
            }
        }
        
             #endregion methods 
        
    }
}
